package com.ruoyi.lock.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lock.domain.ALandlord;
import com.ruoyi.lock.service.IALandlordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 房东信息Controller
 * 
 * @author ruoyi
 * @date 2024-03-08
 */
@RestController
@RequestMapping("/lock/landlord")
public class ALandlordController extends BaseController
{
    @Autowired
    private IALandlordService aLandlordService;

    /**
     * 查询房东信息列表
     */
//    @PreAuthorize("@ss.hasPermi('lock:landlord:list')")
    @GetMapping("/list")
    public TableDataInfo list(ALandlord aLandlord)
    {
        startPage();
        List<ALandlord> list = aLandlordService.selectALandlordList(aLandlord);
        return getDataTable(list);
    }

    /**
     * 查询房东信息列表
     */
//    @PreAuthorize("@ss.hasPermi('lock:landlord:list')")
    @GetMapping("/list1")
    public TableDataInfo list1(ALandlord aLandlord)
    {
        List<ALandlord> list = aLandlordService.selectALandlordList1(aLandlord);
        return getDataTable(list);
    }

    /**
     * 导出房东信息列表
     */
    @PreAuthorize("@ss.hasPermi('lock:landlord:export')")
    @Log(title = "房东信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ALandlord aLandlord)
    {
        List<ALandlord> list = aLandlordService.selectALandlordList(aLandlord);
        ExcelUtil<ALandlord> util = new ExcelUtil<ALandlord>(ALandlord.class);
        util.exportExcel(response, list, "房东信息数据");
    }

    /**
     * 获取房东信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('lock:landlord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(aLandlordService.selectALandlordById(id));
    }


    /**
     * 新增房东信息
     */
    @PreAuthorize("@ss.hasPermi('lock:landlord:add')")
    @Log(title = "房东信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ALandlord aLandlord)
    {
        //在hx中授权APP
        JSONObject jsonObject = null;
        try {
            // 将 Date 转换为 Unix 时间戳（毫秒）
            long begin = aLandlord.getStartTime().getTime();
            long end = aLandlord.getEndTime().getTime();
            // 将毫秒转换为秒
            Long beginLongTime = begin / 1000L;
            Long endLongTime = end / 1000L;

            int i = 0;
            int resultCode = (int) jsonObject.get("resultCode");
            if (resultCode == 0){
                i = aLandlordService.insertALandlord(aLandlord);
                return toAjax(i);
            }else {
                return AjaxResult.error(jsonObject.get("reason").toString());
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return AjaxResult.error(ex.toString());
        }
    }

    /**
     * 修改房东信息
     */
    @PreAuthorize("@ss.hasPermi('lock:landlord:edit')")
    @Log(title = "房东信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ALandlord aLandlord)
    {
        return toAjax(aLandlordService.updateALandlord(aLandlord));
    }

    /**
     * 删除房东信息
     */
    @PreAuthorize("@ss.hasPermi('lock:landlord:remove')")
    @Log(title = "房东信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return AjaxResult.error();
    }

    /**
     * 查询出用户数量
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('lock:landlord:list')")
    @GetMapping("/selectUserCount")
    public Map<String, Integer> selectUserCount()
    {
        Map<String, Integer> userCount = aLandlordService.selectUserCount();
        return userCount;
    }
}
